package com.sky.zzh.home;

import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.eroad.offer.sina.SinaLogin;
import com.eroad.offer.sina.WeiXinLogin;
import com.eroad.offer.tencent.TencentLogin;
import com.next.intf.ITaskListener;
import com.next.net.SHGet;
import com.next.net.SHPut;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHToast;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.R;
import com.sky.zzh.SHApplication;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
import com.sky.zzh.view.SHEditTextNoClear;
import com.tencent.mm.sdk.openapi.IWXAPI;

public class LoginActivity extends Activity implements ITaskListener {

	private SHEditTextNoClear mEtUsername, mEtPwd;
	private Button mBtnLogin, mBtnRegister;
	private TextView mTvForget;
	private SHGet loginTask;
	private SHGet codeTask;
	private SHPut updateTask;
	private String code, phone;
	private ImageView mIvQQ,mIvSina,mIvWeixin;
	IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		mBtnLogin = (Button) findViewById(R.id.btn_login);
		mBtnRegister = (Button) findViewById(R.id.btn_register);
		mTvForget = (TextView) findViewById(R.id.tv_forget);
		mEtUsername = (SHEditTextNoClear) findViewById(R.id.et_username);
		mEtPwd = (SHEditTextNoClear) findViewById(R.id.et_password);
		mIvQQ = (ImageView) findViewById(R.id.iv_qq);
		mIvSina = (ImageView) findViewById(R.id.iv_sina);
		mIvWeixin = (ImageView) findViewById(R.id.iv_weixin);
		setListeners();
		SHApplication.getInstance().addActivity(this);
	}

	private void setListeners() {
		mBtnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (CommonUtil.isEmpty(mEtUsername.getText().toString().trim())) {
					SHToast.showToast(LoginActivity.this, "请输入用户名");
					return;
				}
				if (CommonUtil.isEmpty(mEtPwd.getText().toString().trim())) {
					SHToast.showToast(LoginActivity.this, "请输入密码");
					return;
				}
				SHDialog.ShowProgressDiaolg(LoginActivity.this);
				loginTask = new SHGet();
				loginTask.setListener(LoginActivity.this);
				loginTask.setUrl(ConfigDefinition.URL + "user/UserLogin");
				loginTask.getTaskArgs().put("UserIDOrigianal", mEtUsername.getText().toString().trim());
				loginTask.getTaskArgs().put("Password", mEtPwd.getText().toString().trim());
				loginTask.start();
			}
		});

		mBtnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this, GetCodeActivity.class);
				intent.putExtra("loginType", "meddo99.com");
				intent.putExtra("openid", "");
				startActivity(intent);
			}
		});

		mTvForget.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog dia = new Dialog(LoginActivity.this, R.style.DialogStyle);
				dia.setContentView(R.layout.dia_forget);
				final EditText mEtPhone = (EditText) dia.findViewById(R.id.et_phone);
				dia.findViewById(R.id.tv_cancel).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						dia.dismiss();
					}
				});
				dia.findViewById(R.id.tv_confirm).setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if (CommonUtil.isEmpty(mEtPhone.getText().toString().trim())) {
							SHToast.showToast(LoginActivity.this, "请输入手机号码");
							return;
						}
						phone = mEtPhone.getText().toString().trim();
						code = "" + (int) (Math.random() * 900000 + 100000);
						SHDialog.ShowProgressDiaolg(LoginActivity.this);
						updateTask = new SHPut();
						updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
						updateTask.setListener(LoginActivity.this);
						updateTask.getTaskArgs().put("UserID", "meddo99.com$"+phone);
						updateTask.getTaskArgs().put("Password", code);
						updateTask.start();
						dia.dismiss();
					}
				});
				dia.show();
			}
		});
		mIvQQ.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if(CommonUtil.isInstalled(LoginActivity.this, "com.tencent.mobileqq")){
					SHDialog.ShowProgressDiaolg(LoginActivity.this, "正在跳转QQ...");
//					ConfigDefinition.LoginType = "qq";
					UserInfoManager.getInstance().setLoginType("qq");
					UserInfoManager.getInstance().sync(LoginActivity.this, true);
					new TencentLogin(LoginActivity.this).login();
				}else{
					new SweetDialog(LoginActivity.this, SweetDialog.NORMAL_TYPE).setTitleText("提示").setContentText("尚未安装QQ").show();
				}
				
			}
		});
		
		mIvSina.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(CommonUtil.isInstalled(LoginActivity.this, "com.sina.weibo")){
					SHDialog.ShowProgressDiaolg(LoginActivity.this, "正在跳转微博...");
//					ConfigDefinition.LoginType = "sina";
					UserInfoManager.getInstance().setLoginType("sina");
					UserInfoManager.getInstance().sync(LoginActivity.this, true);
					new SinaLogin(LoginActivity.this,false).sinaLogin();
				}else{
					new SweetDialog(LoginActivity.this, SweetDialog.NORMAL_TYPE).setTitleText("提示").setContentText("尚未安装新浪微博").show();
				}
				
			}
		});
		
		mIvWeixin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(CommonUtil.isInstalled(LoginActivity.this, "com.tencent.mm")){
					SHDialog.ShowProgressDiaolg(LoginActivity.this, "正在跳转微信...");
//					ConfigDefinition.LoginType = "weixin";
					UserInfoManager.getInstance().setLoginType("weixin");
					UserInfoManager.getInstance().sync(LoginActivity.this, true);
					new WeiXinLogin(LoginActivity.this,false).weixinLogin();
				}else{
					new SweetDialog(LoginActivity.this, SweetDialog.NORMAL_TYPE).setTitleText("提示").setContentText("尚未安装微信").show();
				}
				
				
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SHDialog.dismissProgressDiaolg();
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
		System.out.println(task.getResult());
		if(task == codeTask){
			SHToast.showToast(LoginActivity.this, "新密码已发送至手机");
		}else{
			JSONObject json = new JSONObject(task.getResult().toString());
			if ("200".equals(json.optString("ReturnCode"))) {

				if (task == loginTask) {
					UserInfoManager.getInstance().setUserId(json.optString("UserID"));
					UserInfoManager.getInstance().sync(LoginActivity.this, true);
//					ConfigDefinition.LoginType = "normal";
					UserInfoManager.getInstance().setLoginType("normal");
					UserInfoManager.getInstance().sync(LoginActivity.this, true);
					ConfigDefinition.photourl = ConfigDefinition.URL+"file/DownloadFile/"+UserInfoManager.getInstance().getUserId();
					Intent intent = new Intent(LoginActivity.this, ZZHMainActivity.class);
					startActivity(intent);
					finish();
				} else if (task == updateTask) {
					codeTask = new SHGet();
					codeTask.setUrl("http://sdk4report.eucp.b2m.cn:8080/sdkproxy/sendsms.action?cdkey=6SDK-EMY-6688-KGVMO&password=585015&phone="+phone+"&message=您的新密码是"+code+"&addserial=10010");
//					codeTask.getTaskArgs().put("cdkey", "6SDK-EMY-6688-KGVMO");
//					codeTask.getTaskArgs().put("password", "585015");
//					codeTask.getTaskArgs().put("phone", phone);
//					codeTask.getTaskArgs().put("message", "您的新密码是" + code);
//					codeTask.getTaskArgs().put("addserial", "10010");
					codeTask.setListener(LoginActivity.this);
					codeTask.start();
				} 

			} else {
				new SweetDialog(LoginActivity.this, SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText("用户名或密码错误").show();
			}
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub

	}

}
